package com.ysjy.course.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.PromptMessage;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import com.ysjy.course.model.*;
import com.ysjy.course.service.CourseService;
import com.ysjy.course.service.OrderService;
import com.ysjy.course.service.YsSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "场地管理")
@RequestMapping("/site/manage")
@Slf4j
public class SiteManageController extends BaseController {

    @Autowired
    private YsSiteService siteService;

    @ApiOperation(value = "设备计数测试",notes = "{\"deviceNumber\":\"S123456\",\"count\":50,\"date\":1658918255363}")
    @RequestMapping(value = "putDeviceTest", method = RequestMethod.POST)
    public ResultDTO<Boolean> putDeviceTest(
            @Valid @RequestBody YsDeviceCount data){
        try {
            return siteService.putDeviceTest(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }

    @ApiOperation(value = "添加 or 编辑场地",notes = "{\"title\":\"操场\",\"address\":\"紫城中学操场\",\"orgId\":1452,\"tagId\":23,\"useSimultaneously\":true,\"enabled\":true}")
    @RequestMapping(value = "putSite", method = RequestMethod.POST)
    public ResultDTO<Boolean> putSite(
            @Valid @RequestBody YsSite data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return siteService.putSite(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }


    @ApiOperation(value = "查询数量(启用数量)")
    @RequestMapping(value = "getSiteCount", method = RequestMethod.GET)
    public ResultDTO<Map<String, Object>> getSiteCount(
            @ApiParam(value = "组织ID", defaultValue = "1", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }
        try {
            HashMap<String, Object> params = new HashMap<>();
            if (null != orgId && 1 != orgId) {
                params.put("orgId",orgId);
            }
            return new ResultDTO<>(siteService.getSiteCount(params));
        } catch (Exception e) {
            log.error("***********ClassInfoManageController.page******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "场地分页列表")
    @RequestMapping(value = "getSiteListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<YsSite>> getSiteListByPage(
            @ApiParam(value = "机构ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "名称（模糊搜索）", required = false) @RequestParam(name = "title", required = false) String title,
            @ApiParam(value = "场地类型", required = false) @RequestParam(name = "tagId", required = false) Long tagId,
            @ApiParam(value = "状态 1启用 0禁用", required = false) @RequestParam(name = "enabled", required = false) Boolean enabled,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(title)) params.put("title", title);
            params.put("enabled",enabled);
            params.put("tagId",tagId);
            params.put("orgId",orgId);
            PageInfo<YsSite> pageInfo = siteService.getListByPage(params, pageNo, pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取场地列表（课程关联时加载使用）")
    @RequestMapping(value = "getSiteList", method = RequestMethod.GET)
    public ResultDTO<List<YsSite>> getSiteList(
            @ApiParam(value = "机构ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "名称（模糊搜索）", required = false) @RequestParam(name = "title", required = false) String title) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(title)) params.put("title", title);
            params.put("orgId",orgId);
            List<YsSite> pageInfo = siteService.getListByMapParams(params);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "删除场地")
    @RequestMapping(value = "delSite", method = RequestMethod.POST)
    public ResultDTO<Boolean> delSite(
            @ApiParam(value = "唯一标识", required = true) @RequestParam(name = "id", required = true) Long id){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            siteService.deleteByPrimaryKey(id);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }


}